﻿using Microsoft.SharePoint;
using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Linq;
using Microsoft.SharePoint.Utilities;

namespace Portal
{
    [ToolboxItemAttribute(false)]
    public partial class TopNav : WebPart
    {
        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
        // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        public TopNav()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (SPContext.Current.Web.CurrentUser != null)
            {
                DisplayTopNav();
            }
            else
            {
                Page.Response.Redirect("/_layouts/15/Authenticate.aspx?Source=/");
                //SPUtility.RedirectValidate("/_layouts/15/Authenticate.aspx?Source=/", SPContext.Current.Web);
            }
        }

        private void DisplayTopNav()
        {
            SPWeb web = SPContext.Current.Web;
            SPList topNavList = web.GetList("/Lists/TopNav/");

            SPQuery topNavQuery = new SPQuery();

            topNavQuery.Query = @"<Where>
                                  <Eq>
                                     <FieldRef Name='IsActive' />
                                     <Value Type='Boolean'>1</Value>
                                  </Eq>
                               </Where>";
            topNavQuery.ViewFields = @"  
                                            <FieldRef Name='Title'/>
                                            <FieldRef Name='Url'/>                                            
                                            <FieldRef Name='SortOrder'/>
                                            <FieldRef Name='IsActive'/>                                      
                                ";


            SPListItemCollection topNavItems = topNavList.GetItems(topNavQuery);

            if (topNavItems.Count > 0)
            {
                var customerSpeakItemsModified = from topNavItem in topNavItems.GetDataTable().AsEnumerable()
                                                 orderby topNavItem["SortOrder"]
                                                 select new
                                                 {

                                                     Title = topNavItem["Title"].ToString(),
                                                     Url = topNavItem["Url"].ToString()

                                                 };

                lstTopNav.DataSource = customerSpeakItemsModified.ToList();
                lstTopNav.DataBind();
            }
        }
    }
}
